package com.jacklon.weblog.common.utils;

import com.jacklon.weblog.common.enums.ResponseCodeEnum;
import com.jacklon.weblog.common.exception.BaseExceptionInterface;
import com.jacklon.weblog.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {
     // 是否成功，默认为 true
     private boolean success = true;
     // 是否成功，默认为 true
     private String message;
    // 异常码
     private String errorCode;
    // 响应数据
     private T data;

    // =================================== 成功响应 ===================================
     public static <T> Response<T> success() {
         Response<T> response = new Response<>();
         return response;
     }

     public static <T> Response<T> success(T data) {
         Response<T> response = new Response<>();
         response.setData(data);
         return response;
     }

    // =================================== 失败响应 ===================================
     public static <T> Response<T> fail(){
         Response<T> response = new Response<>();
         response.setSuccess(false);
         return response;
     }

     public static <T> Response<T> fail(String errorMessage){
         Response<T> response = new Response<>();
         response.setSuccess(false);
         response.setMessage(errorMessage);
         return response;
     }

    public static <T> Response<T> fail(String errorMessage, String errorCode){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        response.setErrorCode(errorCode);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(bizException.getErrorMessage());
        response.setErrorCode(bizException.getErrorCode());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(baseExceptionInterface.getErrorMessage());
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        return response;
    }
}
